﻿using FAMailUtils;
using FAMailUtils.Data.DAO;
using FAMailUtils.Data.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace OnlineBusinesses.Areas.CustomerAdmin.MailBox
{
    public partial class GroupsMails : System.Web.UI.Page
    {
        GroupDAO dao = new GroupDAO();
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!IsPostBack)
                {
                    lvGroups.DataSource = dao.GetAll(((AdminCustomer_Master)Page.Master).getUserLogin().Client.UserID);
                    lvGroups.DataBind();
                    btnEditSuccess.Visible = false;


                    ClientDAO cdao = new ClientDAO();
                    ClientDTO company = cdao.GetByClientID(((AdminCustomer_Master)Page.Master).getUserLogin().Client.ClientID);
                    lblDomain.Text = "@"+company.CompanyDomain;
                }
            }
            catch (Exception ex)
            {
                Common.logs(Request.RawUrl + "-" + Common.GetCurrentMethod(), ex, Common.logType.error);
            }
        }

        protected void btnCreate_Click(object sender, CommandEventArgs e)
        {
            GroupDTO dto = new GroupDTO();
            dto.GroupName = txtTenNhom.Text;
            dto.GroupMail = txtGroupMail.Text + "@"+lblDomain.Text;
            dto.Description = txtDienGiai.Text;
            if (String.IsNullOrWhiteSpace(dto.GroupName))
            {
                lbNotice.Text = "Tên nhóm không được để trống.";
            }
            else if (String.IsNullOrWhiteSpace(txtGroupMail.Text))
            {
                lbNotice.Text = "Email nhóm không được để trống.";
            }
            else
            {
                string resultCheckName = dao.CheckName(dto.GroupName, 0);

                if (!String.IsNullOrWhiteSpace(resultCheckName))
                {
                    lbNotice.Text = resultCheckName;
                }
                else
                {
                    dto.UserID = ((AdminCustomer_Master)Page.Master).getUserLogin().Client.UserID;
                    dao.Add(dto);
                    Response.Redirect(Request.RawUrl);
                }
            }
        }

        protected void btnDel_Click(object sender, CommandEventArgs e)
        {
            dao.Delete(Convert.ToInt32(e.CommandArgument), ((AdminCustomer_Master)Page.Master).getUserLogin().Client.UserID);
            Response.Redirect(Request.RawUrl);
        }

        protected void btnEdit_Click(object sender, CommandEventArgs e)
        {
            GroupDTO dto = dao.GetByGroupID(Convert.ToInt32(e.CommandArgument), ((AdminCustomer_Master)Page.Master).getUserLogin().Client.UserID);
            txtTenNhom.Text = dto.GroupName;
            txtDienGiai.Text = dto.Description;
            txtGroupMail.Text = dto.GroupMail;
            btnCreate.Visible = false;
            btnEditSuccess.Visible = true;
            btnEditSuccess.CommandArgument = e.CommandArgument.ToString();
        }

        protected void btnEditSuccess_Click(object sender, CommandEventArgs e)
        {
            GroupDTO dto = dao.GetByGroupID(Convert.ToInt32(e.CommandArgument), ((AdminCustomer_Master)Page.Master).getUserLogin().Client.UserID);
            dto.GroupName = txtTenNhom.Text;
            dto.Description = txtDienGiai.Text;
            dto.GroupMail = txtGroupMail.Text;
            if (String.IsNullOrWhiteSpace(dto.GroupName))
            {
                lbNotice.Text = "Tên nhóm không được để trống.";
            }
            else
            {
                string resultCheckName = dao.CheckName(dto.GroupName, Convert.ToInt32(e.CommandArgument));

                if (!String.IsNullOrWhiteSpace(resultCheckName))
                {
                    lbNotice.Text = resultCheckName;
                }
                else
                {
                    resultCheckName = dao.CheckGroupMail(dto.GroupMail, Convert.ToInt32(e.CommandArgument));

                    if (!String.IsNullOrWhiteSpace(resultCheckName))
                    {
                        lbNotice.Text = resultCheckName;
                    }

                    else
                    {
                        dao.Edit(dto);
                        Response.Redirect(Request.RawUrl);
                    }
                }

                btnEditSuccess.Visible = true;
            }
        }
    }
}